<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
class Request extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
        ];
    }

    protected function failedValidation(Validator $validator)
    {
        $error= $validator->errors()->first();
        throw new HttpResponseException($this->fail($error));
    }

    protected function fail($error)
    {
        $result = array(
            'code' => 2,
            'msg' => $error,
            'data' => [],
            'time' => date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']),
        );
        return new \Illuminate\Http\Response($result);
    }
}
